{% extends "base.html" %}
{% block title %}{% endblock %}
{% block content %}

<div class="demoTable">
    <br>
    <form class="layui-form" action="" id="workTable" lay-filter="workTable">

        <div class="layui-input-inline">
            <label class="layui-form-label">project_name：</label>
            <div class="layui-input-inline">
                <select class="layui-select" name="project_id" id="project_id" lay-verify="" lay-search
                                lay-filter="project_id">

                            {% for d in projectAll %}
                                <option value={{d.id}}>{{d.project_name}}</option>
                            {% endfor %}
                </select>
            </div>


                <button  class="layui-btn layui-btn-normal" id="save" lay-submit lay-filter="formDemo">保存</button>


        </div>
        <br><br>
        <div class="layui-input-inline">
            <label class="layui-form-label">param_name：</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" name="param_name" id="param_name" placeholder="请输入" style="width:212px">
            </div>
        </div>
        <br><br>
        <div class="layui-input-inline">
            <label class="layui-form-label">param_type：</label>
            <div class="layui-input-inline">
                <select class="layui-select" name="param_type" id="param_type" lay-verify="" lay-search
                                lay-filter="param_type">
                    <option value="0">全局变量</option>
                    <option value="1">GET请求</option>
                    <option value="2">Response字符关联</option>
                    <option value="3">Response对象取值</option>
                </select>
            </div>
        </div>
        <br><br>
        <div class="layui-input-inline">
            <label class="layui-form-label">param_value：</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" name="param_value" id="param_value" placeholder="请输入" style="width:212px">
            </div>
        </div>

        <br><br>
        <div class="layui-input-inline" id="l_case_id">
            <label class="layui-form-label" >case_id：</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" name="case_id" id="case_id" placeholder="请输入" style="width:212px">
            </div>
        </div>
        <br><br>
         <div class="layui-input-inline"  id="l_jsonObj" >
            <label class="layui-form-label" >PyObj：</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" name="jsonObj" id="jsonObj" placeholder="语法为:response.json().get('data')[0].get('cc')" style="width:212px">
            </div>
        </div>
        <br><br>
        <div class="layui-input-inline" id="l_leftStr">
            <label class="layui-form-label" >leftStr：</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" name="leftStr" id="leftStr" placeholder="请输入" style="width:212px">
            </div>
        </div>
        <br><br>
        <div class="layui-input-inline" id="l_rightStr" >
            <label class="layui-form-label" >rightStr：</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" name="rightStr" id="rightStr" placeholder="请输入" style="width:212px">
            </div>
        </div>
        <br><br>
        <div class="layui-input-inline" id="l_indexStr" >
            <label class="layui-form-label" >indexStr：</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" name="indexStr" value="0" id="indexStr" placeholder="请输入" style="width:212px">
            </div>
        </div>
        <div class="layui-input-inline" style="display:none">
            <div class="layui-input-inline">
                <input type="text" class="layui-input" name="id" id="id" placeholder="请输入" style="width:212px">
            </div>
        </div>



    </form>

</div>
<script>
    layui.use(['table','code'], function(){
          var table = layui.table
          ,form = layui.form;
          var $= layui.jquery;

          apiCaseParams = layui.data('test').apiCaseParams;
          if(apiCaseParams ==undefined){
              form.val('workTable',{
                    param_type:"0",
              });
          }else{
              if (apiCaseParams.where=="manage"){
                    form.val('workTable',{
                        project_id:apiCaseParams.project_id,
                        case_id:apiCaseParams.case_id,
                    });
              }else{
                  param_name = apiCaseParams.param_name
                  param_name = param_name.substr(1);
                  param_name = param_name.substr(0,param_name.length-1);
                   form.val('workTable',{
                        id:apiCaseParams.id,
                        project_id:apiCaseParams.project_id,
                        param_name:param_name,
                        param_type:apiCaseParams.param_type,
                        param_value:apiCaseParams.param_value,
                        case_id:apiCaseParams.case_id,
                        leftStr:apiCaseParams.leftStr,
                        rightStr:apiCaseParams.rightStr,
                        indexStr:apiCaseParams.indexStr,
                        jsonObj:apiCaseParams.jsonObj
                  });
                  $("#project_id").find("option[value='"+apiCaseParams.project_id+"']").attr("selected",true);
                  form.render();
              }
          }

          param_type = $('#param_type option:selected').val();
          if(param_type == "0" || param_type == "1"){
                //$('#case_id').val("");
                $('#l_case_id').hide();

                $('#leftStr').val("");
                $('#l_leftStr').hide();

                $('#rightStr').val("");
                $('#l_rightStr').hide();

                $('#indexStr').val("0");
                $('#l_indexStr').hide();

                $('#jsonObj').val("");
                $('#l_jsonObj').hide();
          }else if(param_type =="3"){
                $('#leftStr').val("");
                $('#l_leftStr').hide();

                $('#rightStr').val("");
                $('#l_rightStr').hide();

                $('#indexStr').val("0");
                $('#l_indexStr').hide();

                $('#l_jsonObj').show();
             }else if(param_type == "2"){

                $('#l_case_id').show();


                $('#l_leftStr').show();

                $('#l_rightStr').show();

                $('#l_indexStr').show();

                $('#jsonObj').val("");
                $('#l_jsonObj').hide();
             }

          form.on('select(param_type)', function(data){
             if(data.value == "0" || data.value == "1" ){
                $('#case_id').val("");
                $('#l_case_id').hide();

                $('#leftStr').val("");
                $('#l_leftStr').hide();

                $('#rightStr').val("");
                $('#l_rightStr').hide();

                $('#indexStr').val("0");
                $('#l_indexStr').hide();

                $('#jsonObj').val("");
                $('#l_jsonObj').hide();


             }else if(data.value =="3"){
                $('#leftStr').val("");
                $('#l_leftStr').hide();

                $('#rightStr').val("");
                $('#l_rightStr').hide();

                $('#indexStr').val("0");
                $('#l_indexStr').hide();

                $('#l_jsonObj').show();
             }else if(data.value == "2"){

                $('#l_case_id').show();


                $('#l_leftStr').show();

                $('#l_rightStr').show();

                $('#l_indexStr').show();

                $('#jsonObj').val("");
                $('#l_jsonObj').hide();
             }
          });

          //监听提交
          form.on('submit(formDemo)', function(data){
            if(data.field.param_name == ""){
                    layer.msg("请输入一个参数名称");
                    return false;
            }

            $.ajax({
                        //发出请求
                        type:"POST",
                        url:"/case/api/params/manage",
                        contentType: 'application/json; charset=utf-8',
                        dataType:"json",
                        data:JSON.stringify(data.field),
                        success: function(result){
                            if(result.id){
                                $('#save').show();
                                layer.msg("保存成功!");
                            }else{
                                layer.msg("参数名称重复!");
                            }
                        }
            });

            return false;
          });

          form.render();

    });

</script>

{% endblock %}